//
//  CenterViewController.m
//  weibo
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CenterViewController.h"
#import "MyButton.h"
#import "AppDelegate.h"
#import "CenterNewFriendVC.h"

#define WH  100
#define TOP 350
#define CR  3
@interface CenterViewController ()
{
    int btnIndex;
}
@property(nonatomic,retain)NSMutableArray *btnArray;

@property (nonatomic, strong) NSTimer *timer;

@property(nonatomic, strong)UIButton *QXButton;

// 好友用户名;
@property (nonatomic, copy)NSString *userName;


@end

@implementation CenterViewController

-(void)viewWillAppear:(BOOL)animated{
    btnIndex = 0;
    // 定时器开启,每隔0.1秒让一个按钮开始动画
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.btnArray = [NSMutableArray array];
//    UIImageView *imageV = [[UIImageView alloc] init];
//    imageV.backgroundColor = [UIColor redColor];
//    imageV.image = [UIImage imageNamed:@""];
//    imageV.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
//    [self.view addSubview:imageV];
    
    self.addFriendButton = [self createButtonWithTitle:@"添加好友" andImage:[UIImage imageNamed:@"publish-video"] andIndex:0];
    [self.addFriendButton addTarget:self action:@selector(addFriendButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.nFriendButton = [self createButtonWithTitle:@"新的朋友" andImage:[UIImage imageNamed:@"publish-picture"] andIndex:1];
    [self.nFriendButton addTarget:self action:@selector(newFriendButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    //待使用按钮
    self.button2 = [self createButtonWithTitle:@"btn2" andImage:[UIImage imageNamed:@"publish-text"] andIndex:2];
    self.button3 = [self createButtonWithTitle:@"btn3" andImage:[UIImage imageNamed:@"publish-audio"] andIndex:3];
    self.button4 = [self createButtonWithTitle:@"btn4" andImage:[UIImage imageNamed:@"publish-review"] andIndex:4];
    self.button5 = [self createButtonWithTitle:@"btn5" andImage:[UIImage imageNamed:@"publish-offline"] andIndex:5];
    
    //取消按钮
    self.QXButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.QXButton.frame = CGRectMake(0, self.view.frame.size.height - 50, self.view.frame.size.width, 50);
    self.QXButton.backgroundColor = [UIColor clearColor];
    [self.QXButton setTitle:@"取消" forState:UIControlStateNormal];
    [self.QXButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:self.QXButton];
    [self.QXButton addTarget:self action:@selector(QXButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // Do any additional setup after loading the view.
}


//button点击方法
- (void)addFriendButtonAction{
    [self buddyRequest];
}

- (void)newFriendButtonAction{
    CenterNewFriendVC *newFriendVC = [[CenterNewFriendVC alloc] init];
//    [self.navigationController pushViewController:newFriendVC animated:YES];
    
    [self presentViewController:newFriendVC animated:YES completion:^{
        
        
    }];
}

- (void)buddyRequest {
    NSLog(@"dsjkfkjlsdfkj");
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"添加好友" message:@"请输入好友用户名" preferredStyle:UIAlertControllerStyleAlert];
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.tag = 12000;
        [textField addTarget:self action:@selector(textAction:) forControlEvents:UIControlEventEditingChanged];
        
    }];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        EMError *error = nil;
        BOOL isSuccess = [[[EaseMob sharedInstance] chatManager] addBuddy:self.userName message:@"我想加你为好友" error:&error];
        NSLog(@"%@",self.userName);
        if (isSuccess && !error) {
            NSLog(@"添加好友成功");
        } else {
            NSLog(@"%d",isSuccess);
            NSLog(@"%@",error);
        }
        
    }]];
    [self presentViewController:alert animated:YES completion:^{
        
        
    }];
}

- (void)textAction:(UITextField *)text {
    self.userName = text.text;
}

- (void)didAcceptedByBuddy:(NSString *)username {
    NSLog(@"======================%@",username);
    NSLog(@"%@",username);
    // 重新获取好友列表刷新tableView
//    [self createData];
    NSLog(@"%@",username);
    // 重新获取好友列表刷新tableView
//    [self createData];
    
}

- (void)didRejectedByBuddy:(NSString *)username {
    NSLog(@"%@",username);
}



//每0.1秒执行一次
- (void)timeChange
{
    
    // 如果当前 btnIndex值为数组的个数就停止
    if (btnIndex == self.btnArray.count) {
        [_timer invalidate];
        return;
    }
    //取出数组中的每个按钮
    UIButton *btn = self.btnArray[btnIndex];
    
    //    usingSpringWithDamping 弹簧效果 值越小弹簧效果越明显
    //    initialSpringVelocity  初始速度就是动画刚开始的速度
    [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        
        // 还原所有按钮坐标到原始位置
        btn.transform = CGAffineTransformIdentity;
        
    } completion:nil];
    
    btnIndex++;
}


-(UIButton *)createButtonWithTitle:(NSString *)title andImage:(UIImage *)image andIndex:(int)index
{
    CGFloat margin = (self.view.frame.size.width - 3 * WH) / (CR + 1);
    
    MyButton *button  = [MyButton buttonWithType:UIButtonTypeCustom];
    
    [button setTitle:title forState:UIControlStateNormal];
    
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [button setImage:image forState:UIControlStateNormal];
    
    // CR是列数  下标%3决定x的坐标  下标/3决定y的坐标
    CGFloat btnX = index % CR * (WH + margin) + margin;
    CGFloat btnY = TOP + index / CR * (WH + margin);
    
    button.frame = CGRectMake(btnX, btnY, WH, WH);
    
    //    button.transform = CGAffineTransformMakeTranslation(0, self.view.bounds.size.height);
    
    // 设置每个按钮偏移位置
    button.transform = CGAffineTransformTranslate(button.transform, 0, self.view.bounds.size.height);
    
    [self.view addSubview:button];
    
    [self.btnArray addObject:button];
    
    return button;
    
}
-(void)QXButtonAction:(UIButton *)button{
    
//    [_timer invalidate];
    _timer = nil;
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

@end
